(function(global){
    'use strict';
    let g;
    if (typeof window !== "undefined") {
        g = window;
    } else if (typeof global !== "undefined") {
        g = global;
    } else if (typeof self !== "undefined") {
        g = self;
    } else {
        g = this;
    }

    const CCSClient = function(){
        let that = {};

        const defaultOptions = {
            reconnect: false,
            secure: false,
            'force new connection': true
        };

        const send = (eventName, data, ack ) => {
            //if(that.socket && that.socket.connected){
            if(that.socket){
                if(data){
                    that.socket.emit(eventName, data, ack);
                } else {
                    that.socket.emit(eventName, ack);
                }
            } else {
                console.log('There is no socket connection or  connection disconnected!');
            }
        }

        const receive = (eventName, ack) => {
            if(that.socket ){
                that.socket.on(eventName, ack);
            } else {
                console.log('There are no cocket connections !');
            }
        }

        // that.init = function(url, options){
        //     that.url = url;
        //     that.option = options || defaultOptions
        // };

        that.connect = (url, options) => {
            const socket = ivcsio.connect(url, options || defaultOptions);
            that.socket = socket;
        };

        that.requestSession = (data, ack) => {
            send('request-session', data, ack);
        };

        that.joinSession = (data, ack) => {
            send('join-session', data, ack);
        };

        that.quitSession = (ack) => {
            send('quit-session', null, ack);
        }

        that.disconnect = () => {
            if(that.socket){
                that.socket.disconnect();
            } else {
                console.log('There is no socket connection!');
            }
        };

        that.close = () => {
          if(that.socket){
            that.socket.close();
          } else {
            console.log('There is no socket connection!');
          }
        }

        /**
         * agent/customr send message
         */
        that.sendMessage = (data, ack) => {
            send('session-message', data, ack);
        }

        that.onMessage = (ack) => {
            receive('session-message', ack)
        }

        that.onDisconnect = (ack) => {
            receive('disconnect', ack)
        }

        that.addEventlistener = (eventName, ack) => {
            receive(eventName, ack);
        }

        that.resumeConnection = (ticket, ack) => {
            send('resume-connection', ticket, ack);
        }

        that.refreshTicket = (ack) => {
            send('refresh-ticket', null, ack);
        }

        that.onConnect = (ack) => {
            receive('connect', ack);
        }

        that.onError = (ack) => {
            receive('error', ack);
        }

        return that;

    }

    const AMSClient = function(){
        let that = {};

        const defaultOptions = {
            reconnect: false,
            secure: false,
            'force new connection': true
        };
        const send = (eventName, data, ack ) => {
            if(that.socket && that.socket.connected){
                if(data){
                    that.socket.emit(eventName, data, ack);
                } else {
                    that.socket.emit(eventName, ack);
                }
            } else {
                console.log('There is no socket connection or  connection disconnected!');
            }
        }

        that.connect = (url, options) => {
            const socket = ivcsio.connect(url, options || defaultOptions);
            that.socket = socket;
        };
        const receive = (eventName, ack) => {
            if(that.socket ){
                that.socket.on(eventName, ack);
            } else {
                console.log('There are no cocket connections !');
            }
        }
        that.login = (data, ack) => {
            send('agent-login', data, ack);
        };
        that.logout = (ack) => {
            send('agent-logout', null, ack);
        };
        that.ready = (ack) => {
            send('agent-ready', null, ack);
        };
        that.unready = (ack) => {
            send('agent-unready', null, ack);
        };
        that.hangup = (ack) => {
          send('agent-hangup', null, ack);
        };
        that.onMessage = (ack) => {
            receive('agent-message', ack);
        }
        that.onRing = (ack)=> {
            receive('ring', ack);
        }
        that.onDisconnect = (ack) => {
          receive('disconnect', ack)
        }
        that.resumeConnection = (ticket, ack) => {
            send('resume-connection', ticket, ack);
        }

        that.refreshTicket = (ack) => {
            send('refresh-ticket', null, ack);
        }

        that.onConnect = (ack) => {
            receive('connect', ack);
        }

        that.onError = (ack) => {
            receive('error', ack);
        }

        that.disconnect = () => {
          if(that.socket){
            that.socket.disconnect();
          } else {
            console.log('There is no socket connection!');
          }
        };

        that.close = () => {
          if(that.socket){
            that.socket.close();
          } else {
            console.log('There is no socket connection!');
          }
        }

        return that;

    }
    // const sioClient = new Client();
    g.ccsClient = CCSClient;
    g.amsClient = AMSClient;
})(window)
